package com.ziyueyuwen.xiniao.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ziyueyuwen.xiniao.bean.ZyClassLessonInfo;
import com.ziyueyuwen.xiniao.bean.trial.TrialClassVO;
import com.ziyueyuwen.xiniao.bean.trial.ZyTrialClassDO;
import com.ziyueyuwen.xiniao.bean.vo.ZyCourseVO;
import com.ziyueyuwen.xiniao.dao.ZyCourseInfoMapper;
import com.ziyueyuwen.xiniao.dao.ZyTrialClassMapper;
import com.ziyueyuwen.xiniao.service.TrialClassService;
import com.ziyueyuwen.xiniao.util.EEOUtil;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.ziyueyuwen.xiniao.util.ConstUtil.KEY_INFO_LIST;
import static com.ziyueyuwen.xiniao.util.ConstUtil.KEY_TOTAL;

/**
 * @author jun 2019/3/7
 */
@Service
public class TrialClassServiceImpl implements TrialClassService {
    @Autowired
    private ZyTrialClassMapper zyTrialClassMapper;

    @Autowired
    private ZyCourseInfoMapper zyCourseInfoMapper;

    @Override
    public Boolean add(ZyTrialClassDO zyTrialClassDO) throws Exception {
        ZyCourseVO zyCourseVO = zyCourseInfoMapper.getByCourseId(zyTrialClassDO.getCourseId());
        Date expiryDate = DateUtils.addDays(zyTrialClassDO.getEndTime(), 1);
        Integer eeoCourseId = EEOUtil.addCourse(zyTrialClassDO.getClassName(), expiryDate,
                zyTrialClassDO.getMasterAccount(), zyCourseVO.getDescription(), zyCourseVO.getImageUrl());
        zyTrialClassDO.setEeoCourseId(eeoCourseId);
        ZyClassLessonInfo zyClassLessonInfo = new ZyClassLessonInfo();
        zyClassLessonInfo.setClassLessonName(zyTrialClassDO.getClassName());
        zyClassLessonInfo.setBeginTime(zyTrialClassDO.getStartTime());
        zyClassLessonInfo.setEndTime(zyTrialClassDO.getEndTime());
        zyClassLessonInfo.setSeatNum(zyTrialClassDO.getSeatNum());
        Integer eeoClassId = EEOUtil.addCourseClass(zyClassLessonInfo, eeoCourseId, zyTrialClassDO.getTeacherAccount(),
                zyTrialClassDO.getTeacherName());
        zyTrialClassDO.setEeoClassId(eeoClassId);
        EEOUtil.editCourse(zyTrialClassDO.getClassName(), expiryDate, eeoCourseId, zyTrialClassDO.getMasterAccount());
        return zyTrialClassMapper.add(zyTrialClassDO);
    }

    @Override
    public Boolean update(ZyTrialClassDO zyTrialClassDO) throws Exception {
        TrialClassVO trialClassVO = zyTrialClassMapper.getById(zyTrialClassDO.getId());
        Date expiryDate = DateUtils.addDays(zyTrialClassDO.getEndTime(), 1);
        zyTrialClassDO.setEeoCourseId(trialClassVO.getEeoCourseId());
        ZyClassLessonInfo zyClassLessonInfo = new ZyClassLessonInfo();
        zyClassLessonInfo.setClassLessonName(zyTrialClassDO.getClassName());
        zyClassLessonInfo.setBeginTime(zyTrialClassDO.getStartTime());
        zyClassLessonInfo.setEndTime(zyTrialClassDO.getEndTime());
        zyClassLessonInfo.setSeatNum(zyTrialClassDO.getSeatNum());
        zyClassLessonInfo.setEeoCourseId(trialClassVO.getEeoCourseId());
        zyClassLessonInfo.setEeoLessonId(trialClassVO.getEeoClassId());
        EEOUtil.editCourseClass(zyClassLessonInfo, zyTrialClassDO.getTeacherAccount(), zyTrialClassDO.getTeacherName());
        EEOUtil.editCourse(zyTrialClassDO.getClassName(), expiryDate, trialClassVO.getEeoCourseId(), zyTrialClassDO.getMasterAccount());
        return zyTrialClassMapper.update(zyTrialClassDO);
    }

    @Override
    public JSONObject listByCourseId(Integer pageNum, Integer pageSize, Integer courseId) {
        JSONObject retObj = new JSONObject();
        Page page = PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" start_time desc ");
        List<TrialClassVO> retList = zyTrialClassMapper.listByCourseId(courseId);
        initDateInfo(retList);
        retObj.put(KEY_TOTAL, page.getTotal());
        retObj.put(KEY_INFO_LIST, retList);
        return retObj;
    }

    @Override
    public TrialClassVO getById(Integer id) {
        TrialClassVO trialClassVO = zyTrialClassMapper.getById(id);
        initDateInfo(trialClassVO);
        return trialClassVO;
    }

    @Override
    public Boolean deleteById(Integer id) throws Exception {
        TrialClassVO trialClassVO = zyTrialClassMapper.getById(id);
        EEOUtil.delCourse(trialClassVO.getEeoCourseId());
        return zyTrialClassMapper.deleteById(id);
    }

    private void initDateInfo(List<TrialClassVO> list) {
        list.forEach(this::initDateInfo);
    }

    private void initDateInfo(TrialClassVO trialClass) {
        Date startTime = trialClass.getStartTime();
        Date endTime = trialClass.getEndTime();
        trialClass.setDateStr(String.format("%tF", startTime));
        trialClass.setTimeStr(String.format("%tR", startTime));
        Integer minute = com.ziyueyuwen.xiniao.util.DateUtil.caleMinute(startTime, endTime, true);
        trialClass.setDurationStr(com.ziyueyuwen.xiniao.util.DateUtil.getDurationString(minute));
    }


}
